import {Component} from '@angular/core';
import {IonicPage, LoadingController, NavController, NavParams, ToastController} from 'ionic-angular';
import {WebcastSearchListPage} from "../webcast-search-list/webcast-search-list";
import {WebCastPlayBackListPage} from "../webcast-playback-list/webcast-playback-list";
import {WebcastIndexProvider} from "../../../providers/webcast/webcast-index/webcast-index.provider";
import {WebcastFavoriteListPage} from "../webcast-favorite-list/webcast-favorite-list";
import {DateService} from "../../../services/common/date.service";
import 'rxjs/add/operator/do';
import * as moment from "moment";
import {WebcastDetailProvider} from "../../../providers/webcast/webcast-detail/webcast-detail";
import {ThemeBrowserService} from "../../../services/native/theme-browser/theme-browser.service";

@IonicPage()
@Component({
    selector: 'page-webcast-index',
    templateUrl: 'webcast-index.html',
    providers: [
        WebcastIndexProvider,
        WebcastDetailProvider,
        ThemeBrowserService,
        DateService
    ]
})
export class WebcastIndexPage {
    /*选择中的视频删选类型 默认前部*/
    selectedVideoType: string = '';
    /*回放视频类型*/
    playVideoTypeList: Array<any> = [];
    /*搜索关键字*/
    keyWord: string;
    /*推荐视频列表数据*/
    recommendVideoList: Array<any> = [];
    /*回放视频列表数据*/
    playBackVideoList: Array<any> = [];
    moment = moment;
    private recommendBgImgUrl: string = './assets/images/webcast/default-live-big.png';
    private playBackBgImgUrl: string = './assets/images/webcast/default-live-small.png';

    constructor(public navCtrl: NavController, public toastCtrl: ToastController,
                public webCastIndexProvider: WebcastIndexProvider,
                private dateService: DateService,
                public loadCtr: LoadingController,
                public themeBrowserService: ThemeBrowserService,
                public navParams: NavParams) {
    }

    ionViewDidLoad() {
        this.getAllVideoList();
    }

    // 头部下拉刷新
    doRefresh(refresher) {
        this.getAllVideoList(false);
        setTimeout(() => {
            refresher.complete();
        }, 1000);
    }

    getAllVideoList(needLoading = true) {
        this.getPlayVideoTypes(needLoading);
        this.getPlayBackVideoList();
    }

    /*获取推荐视频*/
    getRecommendVideoList(needLoading = true) {
        let loading;
        if (needLoading) {
            loading = this.loadCtr.create({
                spinner: 'circles',
                content: "加载中",
            });
            loading.present();
        }
        this.webCastIndexProvider.getRecommendVideoList().subscribe((videoList) => {
            videoList.forEach((item) => {
                item.webcastStartTime = this.dateService.formatCastDate(item.webcastStartTime);
                let imgUrl = item.webcastImgUrl;
                item.subject = '[' + this.getVideoTypeCategory(item.category) + ']' + item.subject;
                item.webcastImgUrl = imgUrl ? imgUrl : this.recommendBgImgUrl;//设置默认图片
            });
            this.recommendVideoList = videoList;
            if (needLoading) {
                loading.dismiss();
            }
        }, () => {
            if (needLoading) {
                loading.dismiss();
            }
        });
    }

    getVideoTypeCategory(type) {
        let typeCategory = '';
        this.playVideoTypeList.forEach((item) => {
            if (item.id == type) {
                typeCategory = item.name;
            }
        });
        return typeCategory;
    }

    /*获取回放视频列表数据*/
    getPlayBackVideoList() {
        this.webCastIndexProvider.getPlayBackVideoList(this.selectedVideoType).subscribe((videoList) => {
            videoList.forEach((item) => {
                let imgUrl = item.webcastImgUrl;
                item.webcastImgUrl = imgUrl ? imgUrl : this.playBackBgImgUrl;//设置默认图片
            });
            this.playBackVideoList = videoList;
        }, () => {
        });
    }

    getPlayVideoTypes(needLoading = true) {
        this.webCastIndexProvider.getPlayBackVideoTypes().subscribe((res) => {
            //由于后台不会传入全部，因此自己加入 全部
            this.playVideoTypeList = [{id: '', name: "全部"}].concat(res);
            this.playVideoTypeList.forEach((item, index) => {
                item.selfIndex = index + 1;
            });
        },()=>{

        },()=>{
            this.getRecommendVideoList(needLoading);
        });
    }


    /*根据删选视频类型，获取回放视频数据*/
    getPlayBackVideosByType(type) {
        this.selectedVideoType = type;
        this.getPlayBackVideoList();
    }

    /*enter搜索*/
    inputKeyUp(e) {
        if (('Enter' === e.key || 13 === e.keyCode)) {
            this.searchByKeyWord();
        }
    }

    searchByKeyWord() {
        let keywordElement: HTMLInputElement = document.querySelector('#keyword') as HTMLInputElement;
        keywordElement.blur();

        let keyWord = this.keyWord;

        if(!keyWord) {
            keyWord = keywordElement.value;
        }

        if (!keyWord) {
            // this.showToast('请输入关键词');
            // return false;
            keyWord = "";
        }
        this.goDetailPage('WebcastSearchListPage', {
            keyWord: keyWord
        });
    }

    /*跳转收藏夹*/
    goCollectionList() {
        this.goDetailPage('WebcastFavoriteListPage');
    }

    /*更多视频跳转*/
    goMoreBackPlayVideos() {
        this.goDetailPage('WebCastPlayBackListPage', {
            type: Number(this.selectedVideoType)
        });
    }


    /*查看一个直播视频*/
    seeLiveVideoDetail(item) {
        this.goDetailPage('WebcastDetailPage', {
            castId: item.id,
            castTitle: item.subject,
            roomId: item.webinarId
        });
    }

    /*查看一个回放视频*/
    seePlayBackVideoDetail(item) {
        this.goDetailPage('WebcastDetailPage', {
            castId: item.id,
            castTitle: item.subject,
            roomId: item.webinarId
        });
    }

    /**
     * 跳转至某页面
     * @param pageName 页面类名
     * @param paramsObj 参数
     */
    goDetailPage(pageName, paramsObj?) {
        this.navCtrl.push(pageName, paramsObj);
    }

    /* 提示框*/
    showToast(msg) {
        let toast = this.toastCtrl.create({
            message: msg,
            position: 'middle',
            duration: 1200
        });
        toast.present();
        toast.dismissAll();
    }
}
